//
//  ViewController.m
//  WChat
//
//  Created by lee on 2018/7/27.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "LoginController.h"
#import "TabbarViewController.h"
#import "ContentViewController.h"

@interface LoginController ()
@property (weak, nonatomic) IBOutlet UITextField *tfName;
@property (weak, nonatomic) IBOutlet UITextField *tfPassword;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - 注册
- (IBAction)regist:(id)sender {
    
//    EMError *error = nil;
//    BOOL isSuccess = [[EaseMob sharedInstance].chatManager registerNewAccount:self.tfName.text password:self.tfPassword.text error:&error];
//    if (!error) {
//        NSLog(@"noError");
//    }
//#warning 不推荐非block，并且注册建议后台服务器进行，有个bug，isSuccess可能返回no但注册成功
//    if (isSuccess) {
//        NSLog(@"注册成功");
//    }
    
    //异步注册
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.tfName.text password:self.tfPassword.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (error) {
            NSLog(@"注册失败%@",error);
            return;
        }
        NSLog(@"注册成功%@==%@",username,password);
    } onQueue:nil];
}

#pragma mark - 登录
- (IBAction)login:(id)sender {
    __weak typeof(self)weakSelf = self;
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.tfName.text password:self.tfPassword.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (error) {
            NSLog(@"登录失败%@",error);
            return;
        }
        NSLog(@"登录成功%@",loginInfo);
        //登录成功后设置自动登录
        [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
        
        ContentViewController *content = [[ContentViewController alloc] init];
        TabbarViewController *tabbar = [TabbarViewController initWithParentController:content];
        [UIApplication sharedApplication].keyWindow.rootViewController = content;
        
    } onQueue:nil];
}

@end
